<?php
/**
 * Zhub - A PHP Framework For API Developer
 *
 * This file is part of the Zhub package.
 *
 * (c) YiLong Fan <fantiq@163.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author fanyilong <fantiq@163.com>
 * @copyright 2017 fanyilong. All rights reserved.
 * @package Zhub
 * @date 18-3-5
 */

namespace Zhub\Route;


use Zhub\HttpMessage\RequestMethod;

class Collection
{
    public $prefix = null;

    public $pipes = [];

    private $parser = null;

    public function __construct(Parser $parser)
    {
        $this->parser = $parser;
    }

    public function addRoute($method, $path, $handle) {
        $path = $this->prefix . '/' . trim($path, '\/');

        $this->parser->parse(new RouteStruct([
            RouteStruct::METHOD => $method,
            RouteStruct::PATH => $path,
            RouteStruct::HANDLE => $handle,
            RouteStruct::PIPES => $this->pipes
        ]));
    }

    public function get(string $path, string $handle, $data = null)
    {
        $this->addRoute(RequestMethod::GET, $path, $handle);
    }
    public function post(string $path, string $handle, $data = null)
    {
        $this->addRoute(RequestMethod::POST, $path, $handle);
    }
    public function head(string $path, string $handle, $data = null)
    {
        $this->addRoute(RequestMethod::HEAD, $path, $handle);
    }
    public function put(string $path, string $handle, $data = null)
    {
        $this->addRoute(RequestMethod::PUT, $path, $handle);
    }
    public function delete(string $path, string $handle, $data = null)
    {
        $this->addRoute(RequestMethod::DELETE, $path, $handle);
    }
    public function options(string $path, string $handle, $data = null)
    {
        $this->addRoute(RequestMethod::OPTIONS, $path, $handle);
    }
}